import React, { Component } from 'react';
import { Button, List, InputItem, WhiteSpace,Toast } from 'antd-mobile';
import { connect } from 'react-redux'
import { Redirect } from 'react-router-dom'
import { regisger } from '../../redux/user.redux'

@connect(
    state => state.user,
    { regisger }
)
class Register extends Component {
    constructor(props) {
        super(props)
        this.state = {
            user: '',
            pwd: ''
        }
    }
    componentWillReceiveProps(nextProps){
        if(nextProps.msg){
            Toast.fail(nextProps.msg, 1);
        }
    }
    handleChange(key, val) {
        this.setState({
            [key]: val
        })
    }
    handleRegister() {
        this.setState({
            msg:null
        })
        this.props.regisger(this.state)
    }

    render() {
        const boss = "luoeji"
        return (
            <div className="App">
                {this.props.redirectTo ? <Redirect to={this.props.redirectTo} /> : null}
                <InputItem
                    placeholder="please input content"
                    data-seed="logId"
                    onChange={v => this.handleChange('user', v)}
                >账号：</InputItem>
                <InputItem
                    placeholder="please input content"
                    data-seed="logId"
                    onChange={v => this.handleChange('pwd', v)}
                >密码：</InputItem>
                <InputItem
                    placeholder="please input content"
                    data-seed="logId"
                >确认密码：</InputItem>
                {this.props.msg?<p className='error-msg'>{this.props.msg}</p>:null}
                <Button type="primary"
                    onClick={() => this.handleRegister()}
                >注册</Button>
                <WhiteSpace />
                <Button type="primary"
                    onClick={() => this.props.history.push('./login')}
                >登录</Button>
            </div>
        );
    }
}

export default Register;
